var Fly = require("@/common/js/request/request")
import websiteConfigs from './../globalConfigs';
import store from '@/store' // 状态管理器
var fly = new Fly;
var newFly = new Fly; // 另外一个请求对象，为了换token用，不与原有对象混用，如果原有对象被锁，就不能换token
fly.config.baseURL = websiteConfigs.baseUrl; /* 站点域名 */
fly.config.headers['content-type'] = 'application/json'  //'application/x-www-form-urlencoded';
// 配置响应拦截器
newFly.config = fly.config;
// 配置请求拦截器，请求拦截器
fly.interceptors.request.use((request) => {
	console.log('request==============',request);
	request.headers["Authorization"] = store.state.userLogin.token ? "Bearer " + store.state.userLogin.token :
		''; // 设置默认请求方式为post
	request.headers['userTest']= '122554'
	if (!request.body || !request.hasOwnProperty('body')) {
		request.body = {}
	};
	request.body.IsWeb = websiteConfigs.isWeb; // 区分端

	return request;
})
/*  响应拦截  */
fly.interceptors.response.use(
	async (res) => { // 响应拦截请重新完善
			return res;
		},
		(err) => {
			//发生网络错误后会走到这里
			fly.unlock()
			uni.hideLoading();
			return Promise.reject('加载错误，请稍后再试')
		}
)
fly.ajax = async function(path, params) {
	let method = params.hasOwnProperty('method') ? params.method.toString().toLowerCase() : 'post';
	delete params.method;
	let flyResponse = await
	fly.request(path, params, {
		method: method,
		timeout: websiteConfigs.requestTimeOut
	})
	return flyResponse;
}

const fetch = fly;
module.exports = {
	fetch
}
